<?php
#flag为0为查询 1为设置
# http://192.168.18.21/master/mcGM/tmp_process/lansel.php?flag=0&device_id=1
# http://192.168.18.21/master/mcGM/tmp_process/lansel.php?flag=1&device_id=2&lansel=1&channel=pcchannel


define("APP_ROOT",dirname(__FILE__) . "/");

include APP_ROOT . "cur_db_cfg.php";

# 解析URL数据
$row = $_GET;

global $conn;
$conn=mysql_connect($dbhost,$dbaccount,$dbpwd);
#var_dump($conn);
mysql_select_db("shouhuzhemen_account") or die('Could not select shouhuzhemen_account');
$flag = $row['flag'];
$rowTmp = array();

if ( $flag == 0 )
{
	GetLanSel($row['device_id'],$rowTmp);
}
else 
{
	UpdateLanSel($row['device_id'],$row['lansel'],$row['channel'],$rowTmp);
}
echo urldecode(json_encode($rowTmp));
//多语言选择改变
function GetLanSel($device_id,&$rowTmp)
{
    $rowTmp['device_id'] = $device_id;
    $rowTmp['sel'] = -1; #默认为-1
    #var_dump($rowTmp['sel']);
    $sql = "SELECT * from device_lan dl where dl.device_id = '" . $device_id . "' LIMIT 1;";
    $result = mysql_query($sql);
	if(false == $result)
	{
	    return;
	}
	
	while ( $line = mysql_fetch_array($result))
	{
		$rowTmp['sel'] = (int)$line['sel'];
		#var_dump($rowTmp['sel']);
	}
	
	mysql_free_result($result);
}


//多语言选择改变
function UpdateLanSel($device_id,$lansel,$channel,&$rowTmp)
{
   /*更新表,找到设备uuid*/
   $rowTmp['device_id'] = $device_id;
   $rowTmp['ret'] = 1;

   	
   $sql = "insert into device_lan(device_id,device_mac,sel,channel_s) values('" . $device_id . "','',$lansel," . "'" . $channel . "') ".
   "on duplicate key update sel = $lansel,device_id = '" . $device_id . "', channel_s = '" . $channel . "';";
   
   $updateRet = mysql_query($sql);
	if ( true == $updateRet )
	{
		$rowTmp['ret'] = 0;
	}
}

?>

